/*
 * Hai Phan
 * 
 * TCSS 342 - Winter 2010
 * Assignment 6B - Spreadsheet
 * March 4, 2010
 */

package gui;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JSeparator;
import javax.swing.JTextField;

/**
 * SpreadSheetGUI, keeps track of the Swing 
 * components on the SpreadSheet GUI, the JTable for the data, 
 * the JFrame, and the JTextField for the formula.
 * 
 * @author Hai Phan
 * @version March 4, 2010
 */
public class SpreadSheetGUI {
	
	/**
	 * The title of the spreadsheet.
	 */
	private static final String TITLE = "Spreadsheet";
	
	/**
	 * The about message.
	 */
	private static final String ABOUT_MSG = "Spreadsheet Application\n" +
			"Written by James Santos-Calacat, Jonathan Fangmeyer, Hai Phan\n" +
			"TCSS 342 - Winter 2010";
	
	/**
	 * The frame of the spreadsheet.
	 */
	private JFrame my_frame;
	
	/**
	 * The spreadsheet table.
	 */
	private SpreadSheetTable my_table_panel;
	
	/**
	 * The textfield in the GUI.
	 */
	private JTextField my_display_formula;
	
	/**
	 * The main GUI.
	 */
	private SpreadSheetGUI my_gui;
	
	/**
	 * SpreadSheetGUI constructor, initializes the 
	 * SpreadSheetGUI with default values.
	 */
	public SpreadSheetGUI() {
		my_frame = new JFrame(TITLE);
		my_display_formula = new JTextField();
		my_table_panel = new SpreadSheetTable(my_display_formula);
		my_gui = this;
	}
	
	/**
	 * Starts the SpreadSheetGUI construction.
	 */
	public void start() {
		setUpFrame();
	}
	
	/**
	 * Sets the new table.
	 * 
	 * @param table the new table.
	 */
	public void setTable(SpreadSheetTable table) {
		my_table_panel = table;
	}
	
	/**
	 * Sets up the frame of the spreadsheet. (add components, set location,
	 * and default close operation).
	 */
	private void setUpFrame() {
		my_frame.add(BorderLayout.CENTER, my_table_panel);
		my_frame.add(BorderLayout.NORTH, my_display_formula);
		my_frame.setJMenuBar(createJMenuBar());
		my_frame.setVisible(true);
		my_frame.pack();
		my_frame.setLocationRelativeTo(null);
		my_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	/**
	 * Create and returns a JMenuBar.  JMenuBar will contain a
	 * File, Option, and a Help JMenu.
	 * 
	 * @return menu_bar a JMenuBar.
	 */
	private JMenuBar createJMenuBar() {
		JMenuBar menu_bar = new JMenuBar();
		menu_bar.add(createFileMenu());
		menu_bar.add(createOptionMenu());
		menu_bar.add(createHelpMenu());
		return menu_bar;
	}
	
	/**
	 * Create and returns a file JMenu.  File JMenu will contain the
	 * exit JMenuItem that allows the user to exit the program, and the
	 * print JMenuItem that allows the user to print the spreadsheet.
	 * 
	 * @return file_menu a file JMenu.
	 */
	private JMenu createFileMenu() {
		JMenu file_menu = new JMenu("File");
		JMenuItem print_item = new JMenuItem("Print...");
		JMenuItem exit_item = new JMenuItem("Exit");
		exit_item.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
				System.exit(0);
			}
		});
		
		print_item.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
				my_table_panel.printTable();
			}
		});
		
		file_menu.add(print_item);
		file_menu.add(new JSeparator());
		file_menu.add(exit_item);
		return file_menu;
	}
	
	/**
	 * Creates and returns an option JMenu.  Option JMenu will contain
	 * the edit JMenuItem for user to change the rows & columns of the 
	 * spreadsheet.
	 * 
	 * @return option_menu an option JMenu.
	 */
	private JMenu createOptionMenu() {
		JMenu option_menu = new JMenu("Option");
		option_menu.add(createEditItem());
		return option_menu;
	}

	/**
	 * Create and returns a help JMenu. Help JMenu will contain
	 * an About JMenuItem.
	 * 
	 * @return help_menu a help JMenu.
	 */
	private JMenu createHelpMenu() {
		JMenu help_menu = new JMenu("Help");
		JMenuItem about_item = new JMenuItem("About");
		about_item.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
				JOptionPane.showMessageDialog(null, ABOUT_MSG, TITLE, 
						JOptionPane.INFORMATION_MESSAGE);
			}
		});
		help_menu.add(about_item);
		return help_menu;
	}

	/**
	 * Create and returns an edit row/column JMenuItem.
	 * 
	 * @return edit_item an edit row/column JMenuItem.
	 */
	private JMenuItem createEditItem() {
		JMenuItem edit_item = new JMenuItem("Set # of Rows & Columns");
		edit_item.addActionListener(new ActionListener() {
			public void actionPerformed(final ActionEvent the_event) {
		    	new EditDialog(my_table_panel, my_display_formula, my_frame, my_gui);
			}
		});
		return edit_item;
	}

}
